﻿using System;
using System.Text;
using System.Net;
using System.Collections.Generic;
using System.IO;

namespace CD2WebAudio3Lib
{
    public class resolve_ftp
    {

        /// <summary>
        /// Resolves the host name and returns the address
        /// </summary>
        /// <param name="pHostNameAndPath"></param>
        /// <param name="pUserName"></param>
        /// <param name="pPassword"></param>
        /// <returns></returns>
        public static string Resolve_FTP_Address(string pHostNameAndPath, string pUserName, string pPassword, string pPort)
        {
            IPHostEntry host;
            string hostName;
            string returnVal = string.Empty;

            if (pPort == "") pPort = "21";

            // remove any leading characters
            if (pHostNameAndPath.ToLower().StartsWith("ftp://"))
                hostName = pHostNameAndPath.Substring(6);
            else
                hostName = pHostNameAndPath;

            // remember the path, if included
            string pathStr = string.Empty;
            int pos = hostName.IndexOf('/');
            if (pos > 0)
            {
                pathStr = hostName.Substring(pos);
                hostName = hostName.Substring(0, pos);
            }

            // get the ip address
            try
            {
                host = Dns.GetHostEntry(hostName);
                returnVal = host.AddressList[0].ToString() + ":" + pPort + pathStr;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Host " + hostName + " was not found.\n\n" + e.Message);
                return null;
            }

            if (pUserName.Length > 0)
            {

                // test connect by listing the directory
                List<string> contents = new List<string>();
                FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(new Uri(ftp_Path(returnVal)));
                ftp.Credentials = new NetworkCredential(pUserName, pPassword);
                ftp.Method = WebRequestMethods.Ftp.ListDirectory;
                using (WebResponse response = ftp.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string line = reader.ReadLine();
                        while (line != null)
                        {
                            contents.Add(line);
                            line = reader.ReadLine();
                        }

                        reader.Close();
                    }
                    response.Close();
                }
            }

            // return the resolved address and path
            return returnVal;

        }

        private static string ftp_Path(string pAddress)
        {
            string path = pAddress;
            if (!path.ToLower().StartsWith("ftp:")) path = @"ftp://" + path;
            if (!path.EndsWith(@"/")) path += @"/";
            return path;
        }
    }
}
